﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace WaterMark
{
    /// <summary>
    /// WaterMark 的摘要说明
    /// </summary>
    ///  
    /// <param name="strCopyright">要加入的文字</param>
    /// <param name="strMarkPath">水印图片路径</param>
    /// <param name="strPhotoPath">要加水印的图片路径</param>
    /// <param name="strSavePath">处理后的图片路径</param>
    /// <param name="iMarkRightSpace">水印在修改图片中距左边的宽度</param>
    /// <param name="iMarkButtomSpace">水印在修改图片中距底部的高度</param>
    /// <param name="iDiaphaneity">水印图片的透明度</param>
    /// <param name="iFontRightSpace">文字</param>
    /// <param name="iFontButtomSpace">文字</param>
    /// <param name="iFontDiaphaneity">文字</param>
    /// <param name="bShowCopyright">是否显示文字</param>
    /// <param name="bShowMarkImage">是否显示水印图片</param>

    public class WaterMark
    {
        private string name = "";

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string sex = "";

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        private string age = "";

        public string Age
        {
            get { return age; }
            set { age = value; }
        }
        private string head = "";

        public string Head
        {
            get { return head; }
            set { head = value; }
        }
        private string oldImg = "";

        public string OldImg
        {
            get { return oldImg; }
            set { oldImg = value; }
        }
        public string newpath = "";

        public WaterMark()
        { }
        public WaterMark(string name, string sex, string age, string head,string oldimg,string newpath)
        {
            this.Name = name;
            this.newpath = newpath;
            this.Age = age;
            this.Sex = sex;
            this.Head = head;
            this.OldImg = oldimg;
        }
        public bool maker()
        {
            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Sex) || string.IsNullOrEmpty(age) || string.IsNullOrEmpty(Head) || string.IsNullOrEmpty(OldImg))
            {
                return false;
            }
            if (!File.Exists(OldImg))
            {
                throw new FileNotFoundException("The file don't exist!");
            }

            if (Name == string.Empty)
            {
                return false;
            }
            //还需要判断文件类型是否为图像类型，这里就不赘述了  

            System.Drawing.Image image = System.Drawing.Image.FromFile(OldImg);
            Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
            Graphics g = Graphics.FromImage(bitmap);

            float fontSize = 12.0f;    //字体大小  
            float textWidth = Name.Length * fontSize;  //文本的长度  
            //下面定义一个矩形区域，以后在这个矩形里画上白底黑字  
            float rectX = 308;
            float rectY = 46;
            float rectWidth = Name.Length * (fontSize + 8);
            float rectHeight = fontSize + 8;
            //声明矩形域  
            RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

            Font font = new Font("黑体", fontSize);   //定义字体  
            Brush whiteBrush = new SolidBrush(Color.Red);   //白笔刷，画文字用  
            Brush blackBrush = new SolidBrush(Color.White);   //黑笔刷，画背景用  

            g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

            g.DrawString(Name, font, whiteBrush, textArea);
            textArea.Y = 65;
            g.DrawString(Sex, font, whiteBrush, textArea);
            textArea.Y = 85;
            g.DrawString(Age, font, whiteBrush, textArea);
            Image headimg= Image.FromFile(Head);
            textArea.X=365;
            textArea.Y=26;
            textArea.Width=100;
            textArea.Height=100;
            g.DrawImage(headimg, textArea);
            //MemoryStream ms = new MemoryStream();
            //保存为Jpg类型  
            bitmap.Save(newpath, ImageFormat.Jpeg);

           
            
            g.Dispose();
            bitmap.Dispose();
            image.Dispose();
            headimg.Dispose();
            if (File.Exists(Head))
                File.Delete(Head);
            return true;
        }
    }
}